package com.dz.common.core.entity.listener;


import com.dz.common.core.entity.BaseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.PostPersist;
import javax.persistence.PostUpdate;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import java.util.Date;

/**
 * 
 * <p>
 * Description:baseEntity监控器
 * </p>
 *
 *
 */
@Configurable
public class EntityListener extends AuditingEntityListener {


    /**
     * 在保存之前调用
     */
    @SuppressWarnings("rawtypes")
    @PrePersist
    public void prePersist(Object obj) {
        if (obj instanceof BaseEntity) {
            BaseEntity baseEntity = (BaseEntity) obj;

            if ((baseEntity).getDeleteFlag() == null) {
                (baseEntity).setDeleteFlag(0);
            }
            if ((baseEntity).getVersion() == null) {
                (baseEntity).setVersion(0);
            }
            (baseEntity).setCreateTime(new Date());

            (baseEntity).setUpdateTime(new Date());

            try {
                if ((baseEntity).getCreator() == null) {
             /*       if (apiStorage.get().getUserId() == null) {
                        (baseEntity).setCreator(0l);
                    } else {
                        (baseEntity).setCreator(apiStorage.get().getUserId());
                    }*/
                    (baseEntity).setCreator(0L);
                }
                if ((baseEntity).getUpdater() == null) {
                  /*  if (apiStorage.get().getUserId() == null) {
                        (baseEntity).setUpdater(0L);
                    } else {
                        (baseEntity).setUpdater(apiStorage.get().getUserId());
                    }*/
                    (baseEntity).setUpdater(0L);
                }
            } catch (Exception e) {
                (baseEntity).setCreator(0L);
                (baseEntity).setUpdater(0L);
            }
        }
    }

    /**
     * 在保存之后调用
     */
    @PostPersist
    public void postPersist(Object obj) {
        // postPersist 不做处理
    }

    /**
     * 在保存之前调用
     */
    @SuppressWarnings("rawtypes")
    @PreUpdate
    public void preUpdate(Object obj) {
        BaseEntity baseEntity = (BaseEntity) obj;
        try {
          /*  if (apiStorage.get().getUserId() == null) {
                (baseEntity)
                        .setUpdater(baseEntity.getUpdater() == null ? 0L : baseEntity.getUpdater());
            } else {
                (baseEntity).setUpdater(apiStorage.get().getUserId());
            }*/
            (baseEntity)
                    .setUpdater(baseEntity.getUpdater() == null ? 0L : baseEntity.getUpdater());
        } catch (Exception e) {
            (baseEntity).setUpdater(0L);
        }
        (baseEntity).setUpdateTime(new Date());
    }

    /**
     * 在保存之后调用
     */
    @PostUpdate
    public void postUpdate(Object obj) {
        // postUpdate 不做处理
    }
}
